<?php
namespace Home\Controller;

use Think\Controller;

/**
*@desc 服务商后台
*/
class PrushController extends CommonController {
    private $auth_status = 0;//默认：未认证
    private $user_email = null;//用户邮箱
    private $uid = null;
    private $auth_type = 1;
    private $user_name = '';
    private $white_list = array('shipeng.hou@37degree.com');//白名单，修改uri权限
    private $prush_to_uid = 999999999;//提现到公司账户uid

    public function __construct(){
        //是否服务商用户，且登录状态
        parent::__construct();

		// //判断用户是否服务商用户
  //       if($this->role==1){
  //           redirect('/Index/index');
  //       }
        
        $this->getAuthStatus();
        $this->auth_status = session('37degree_auth_status');
        
        $this->user_email = session('user_email');
        $this->uid = session('uid');
        $this->auth_type = session('auth_type');
        $this->user_name = session('user_name');
        $this->prush_to_uid = C('ADMIN_UID');

        $this->assign('user_email',$this->user_email);
        if(empty($this->uid)) redirect('/member/login.html?role=company');
    }

    //我的钱包
    public function index(){
        $uservaultModel = D('Uservault');
        $info = $uservaultModel->getInfo($this->uid);
        $this->assign('info',$info);
        $this->assign('head_title','我的钱包－云途数据市场');
        $this->display();
    }

    //提现详情
    public function info(){
        $id = I('get.id',0,'intval');
        if(empty($id) || !$id || !is_numeric($id)) $this->error('缺少必要参数','/prush/index.html');

        //获取提现金额
        $withdrawalsModel = D('WithdrawalsHistory');
        $info = $withdrawalsModel->getInfo($this->uid,$id);
        
        if(!empty($info)){
            $info['auth_path'] = explode('->',$info['auth_path']);
            if($info['review_status']==-1){
                $msg = json_decode($info['failed_msg'],true);
                $failed_msg = '';
                foreach ($msg as $k=>$v){
                    $failed_msg = $v."\n";
                }
                $info['failed_msg'] = $failed_msg;
            }
        }
        // echo "<pre>";print_r($info);die;
        $this->assign('info',$info);
        $this->assign('head_title','提现详情－云途数据市场');
        $this->display();
    }

    //提现记录
    public function ajaxGetPrushList(){
        if(empty($this->uid)) $this->ajaxReturn( array('status'=>4001,'msg'=>'请登录','data'=>null));
        //withdrawals
        $page = I('post.page',1,'intval');
        $pagesize = I('post.pagesize',20,'intval');

        $withdrawalsModel = D('WithdrawalsHistory');
        // $withdrawalsModel
        $data = $withdrawalsModel->getList($this->uid,$page,$pagesize);

        $this->ajaxReturn( array('status'=>'200','msg'=>'Ok','data'=>$data)); 
    }

    //收支明细
    public function ajaxGetBalanceList(){
        if(empty($this->uid)) $this->ajaxReturn( array('status'=>4001,'msg'=>'请登录','data'=>null));
        $page = I('post.page',1,'intval');
        $pagesize = I('post.pagesize',20,'intval');

        $paymentsModel = D('PaymentsHistory');
        $data = $paymentsModel->getPaymentsList($this->uid,$page,$pagesize);

        $this->ajaxReturn( array('status'=>$status,'msg'=>$msg,'data'=>$data));
    }

    //提现 todo
    public function cashpostal(){
        //余额 //可用金额
        $uservaultModel = D('Uservault');
        $info = $uservaultModel->getInfo($this->uid);
        $this->assign('info',$info);
        // echo "<pre>";print_r($info);

        //不可用金额（正在审核，提现金额）
        $withdrawalsModel = D('WithdrawalsHistory');
        $ing_list = $withdrawalsModel->getIngList($this->uid);
        $ing_amount = 0;
        $last_info = array();
        if(!empty($ing_list)){
            foreach ($ing_list as $k=>$v){
                $ing_amount += $v['cash_amount'];
            }

            $last_info = $ing_list[count($ing_list)-1];
        }
        $this->assign('ing_amount',$ing_amount);

        $this->assign('last_info',$last_info);
        // echo "<pre>";print_r($last_info);die;
        $this->assign('head_title','提现－云途数据市场');
        $this->display();
    }

    //处理“提现”ajax处理
    public function ajaxCashpostal(){
        if(empty($this->uid)){
            $this->ajaxReturn( array('status'=>4001,'msg'=>'请登录','data'=>null));
        }
        //参数过滤
        $cash_amount = I('post.amount',null);
        if(empty($cash_amount) || !is_numeric($cash_amount)){
            $this->ajaxReturn( array('status'=>4006,'msg'=>'请填写可提现之内金额','data'=>null));
        }
        $bank_name = I('post.bankname',null);
        if(empty($bank_name)){
            $this->ajaxReturn( array('status'=>4006,'msg'=>'请填写银行信息','data'=>null));   
        }
        $card_no = I('post.cardno',null);
        if(empty($card_no)){
            $this->ajaxReturn( array('status'=>4006,'msg'=>'请填写银行卡号信息','data'=>null));   
        }

        $init_bank_name = I('post.init_bankname',null);
        if(empty($init_bank_name)){
            $this->ajaxReturn( array('status'=>4006,'msg'=>'请填写开户行','data'=>null));   
        }

        $card_user_name = I('post.card_username',null);
        if(empty($card_user_name)){
            $this->ajaxReturn( array('status'=>4006,'msg'=>'请填写户主名称','data'=>null));   
        }

        //检查用户金额是否大于可用金额
        $uservaultModel = D('Uservault');
        $info = $uservaultModel->getInfo($this->uid);
        $this->assign('info',$info);

        if($info['amount'] < $cash_amount){
            $this->ajaxReturn( array('status'=>4006,'msg'=>'请填写可提现之内金额','data'=>null));   
        }

        //开启事务
        $userModel = M('UserVault');
        $userModel->startTrans();
        //金额处理“减”
        $vault_where['uid'] = $this->uid;
        $vault_result = $userModel->where($vault_where)->setDec('amount',$cash_amount);

        //提现记录表(提现状态)
        $withdrawalsModel = M('WithdrawalsHistory');
        $withdrawals_data = array(
            'uid'=>$this->uid,
            'user_name'=>$this->user_name,
            'cash_amount'=>$cash_amount,
            'bank_name'=>$bank_name,
            'card_no'=>$card_no,
            'init_bank_name'=>$init_bank_name,
            'card_user_name'=>$card_user_name,
            'review_status'=>0,
            'cash_time'=>time()
        );
        $withdrawals_result = $withdrawalsModel->data($withdrawals_data)->add();

        //收支明细表
        $paymentsModel = M('PaymentsHistory');
        $pay_data = array(
            'from_uid'=>$this->uid,
            'from_username'=>$this->user_name,
            'to_uid'=>$this->prush_to_uid,
            'amount'=>$cash_amount,
            'payments_type'=>-1,
            'payments_reason'=>'提现',
            'create_time'=>time()
        );
        $pay_his_result = $paymentsModel->data($pay_data)->add();

        if ($vault_result && $withdrawals_result && $pay_his_result){
            // 提交事务
            $userModel->commit();
            $this->ajaxReturn( array('status'=>200,'msg'=>'操作成功','data'=>array('withdrawals_id'=>$withdrawals_result)));
        }else{
            $userModel->rollback();
            $this->ajaxReturn( array('status'=>4007,'msg'=>'操作失败','data'=>null));
        }
    }

}
